﻿using System.Collections.Generic;
using System.Net.Sockets;

namespace Cmpp.Client.Cmpp3Message.Utility {
    internal class SocketAsyncEventArgsPool {
        private readonly Stack<SocketAsyncEventArgs> m_stack;
        private readonly object m_syncLock = new object();

        public SocketAsyncEventArgsPool(int capacity) {
            m_stack = new Stack<SocketAsyncEventArgs>(capacity);
        }

        public int Count {
            get { return m_stack.Count; }
        }

        public void Push(SocketAsyncEventArgs item) {
            lock (this.m_syncLock) {
                if(item.AcceptSocket != null) {
                    item.AcceptSocket = null;
                }
                m_stack.Push(item);
            }
        }

        public SocketAsyncEventArgs Pop() {
            lock (this.m_syncLock) {
                return m_stack.Pop();
            }
        }
    }
}